package stvd.sign;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.text.ParseException;

import org.springframework.security.crypto.codec.Base64;
import org.stvd.common.Consts;
import org.stvd.core.util.DateUtil;

public class SignProvider { 
	public final static String pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0Fnr1rEwp0El3TbtyLWA4mPGG1FQsAI4t/ku9EiTxEFUH1DmJBtkj2He4g3O0KQTqOXbvBV8uETKIMeZAnQ1u2M4SYZZ2AmLl/GYPTd1j6+c1SGPc0Pffmcc6lrjxhFx5rYilgQ3cBgmjIpfrKsapmnUjk5TE+J28KjHdiMiweQIDAQAB";
	
	private String version = "20161220";
	private String userName = "stvd";
	private String createDate = "20161220";
	private String expiresDate = "20180330";
	private String serialNum = Consts.GUID;
	private String signText = "vtYpxmrw0bK0GRyUbyLfxRkjgBVsEUSZl3CMYVt52EkQ0ryfbjD5PtZk6CPJSMTFWykPMczPjqSLoXT74mt5Iya1YJWiLUQVBvaVOFFkJHtXTwy46iAPyNomxtmnZo+FkJ2AU/Ye+B6o85gfSn2ekGZpJckj7ntRZ2T5EK0Si1s=" ;
//	public String getExpiresDate(){
////		return licenseMap.getProperty("expires_date") ;
////		String version = licenseMap.getProperty("version") ;
//		Security sec = new Security();
//		String version = sec.getCode();
//		version = new String(Base64.decode(version.getBytes()));
//		System.out.print(version);
//		return version.substring(version.length() - 8);
//	}
	
	public String getVersion() {
		return version;
	}

	public String getSignText() {
		return signText;
	}

	public void setSignText(String signText) {
		this.signText = signText;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getCreateDate() {
		return createDate;
	}

	public void setCreateDate(String createDate) {
		this.createDate = createDate;
	}

	public String getExpiresDate() {
		return expiresDate;
	}

	public void setExpiresDate(String expiresDate) {
		this.expiresDate = expiresDate;
	}

	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	public String getLicenseText(){
		StringBuilder sb = new StringBuilder();
		sb.append(getVersion());
		sb.append(getSerialNum());
		sb.append(getUserName());
		sb.append(getCreateDate());
		sb.append(getExpiresDate());
//		
		return new String(Base64.encode(sb.toString().getBytes()));
	}
	
	public static boolean verify(byte[] pubKeyText, String plainText, byte[] signText) { 
		
		try { 
			// 解密由base64编码的公钥,并构造X509EncodedKeySpec对象 
			X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64.decode(pubKeyText)); 
			// RSA对称加密算法 
			KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
			// 取公钥匙对象 
			PublicKey pubKey = keyFactory.generatePublic(bobPubKeySpec); 
			// 解密由base64编码的数字签名 
			byte[] signed = Base64.decode(signText); 
			Signature signatureChecker = Signature.getInstance("MD5withRSA"); 
			signatureChecker.initVerify(pubKey); 
			signatureChecker.update(plainText.getBytes()); 
			// 验证签名是否正常 
			if (signatureChecker.verify(signed)) 
				return true; 
			else 
				return false; 
		} catch (Throwable e) { 
			System.out.println("校验签名失败"); 
			e.printStackTrace(); 
			return false;
		} 
		
//		return true ;
	}
	
	public static boolean verify(){
		SignProvider signProvider = new SignProvider();
		
		try {
			if(!DateUtil.beforDate2(DateUtil.getSystemDateOfY2D(),signProvider.getExpiresDate())){
				return false ;
			} else {
				return SignProvider.verify(pubKey.getBytes(), signProvider.getLicenseText(), signProvider.getSignText().getBytes());
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return false ;
		}
		
//		KeyGenerater keyGenerater = new KeyGenerater() ;
//		keyGenerater.setSeed(signProvider.getSerialNum()+signProvider.getExpiresDate()+signProvider.getUserName());
//		keyGenerater.generater();
//		return SignProvider.verify(keyGenerater.getPubKey(), signProvider.getLicenseText(), signProvider.getSignText().getBytes());
	}
	
	public static void main(String[] args) {
		SignProvider signProvider = new SignProvider();
		//生成liText
		System.out.println(signProvider.getLicenseText());
	}
}